package za.org.meraka.mxitstem.index;

import java.util.Properties;
import java.io.InputStream;
import java.io.File;


import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.FSLockFactory;

import za.org.meraka.mxitstem.analysis.MxitAnalyzer;


public class Indexer {

Properties props;
IndexWriter indexWriter;

private void makeIndexDirectory(String dirname) {
	try {
		File file = new File(dirname);
		if ( !file.exists() ) {
			file.mkdirs();
			System.out.println(dirname + " created");
		}
		System.out.println(dirname + " exists");
		if ( !file.isDirectory() ) {
			System.out.println(dirname + " exists but is not a directory!!!");
			System.exit(0);
		}

		String files[] = file.list();
		if ( files == null || files.length == 0 ) {
			System.out.println("there are no files");
			indexWriter = new IndexWriter(FSDirectory.open(file), new MxitAnalyzer(), //true,
						IndexWriter.MaxFieldLength.UNLIMITED);
			System.out.println("indexwriter created");
		}
		else {
			System.out.println("there are already files in " + dirname);
		}
	}
	catch ( Throwable t ) {
	}
}

public Indexer(String args[]) {
	System.out.println("indexer constructor");
	try {
		InputStream propStream = Indexer.class.getResourceAsStream("/mxitstem.properties");
		System.out.println("got property stream");
		props = new Properties();
		props.load(propStream);
		System.out.println("properties loaded");
		System.out.println("data.dir " + props.get("data.dir"));
		System.out.println("index.dir " + props.get("index.dir"));
		makeIndexDirectory((String)props.get("index.dir"));
	}
	catch ( Throwable t ) {
		System.out.println("Indexer constructor throwing " + t);
	}
}

public static void main(String args[]) {
	System.out.println("Indexer running");
	Indexer indexer = new Indexer(args);
}

}
